﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using park.Common;

namespace park.Location
{
    /// <summary>
    /// Proxy 的摘要说明
    /// </summary>
    public class Proxy : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpRequest Request = context.Request;
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            string method = Request.Params["method"];

            string returnString = string.Empty; // 返回结果

            switch (method)
            {
                case "listParkingSpaces":
                    {
                        returnString = ListParkingSpaces(Request, sl);
                        break;
                    }
                case "DeleteParkingSpace":
                    {
                        returnString = DeleteParkingSpace(Request, sl);
                        break;
                    }
                case "ListBerthModels":
                    {
                        returnString = ListBerthModels(sl);
                        break;
                    }
                case "ListBerthType":
                    {
                        returnString = ListBerthType(sl);
                        break;
                    }
                case "ListRestrictions":
                    {
                        returnString = ListRestrictions(sl);
                        break;
                    }
                case "ListFeeRules":
                    {
                        returnString = ListFeeRules(sl);
                        break;
                    }
                case "ListContinuousLong":
                    {
                        returnString = ListContinuousLong(sl);
                        break;
                    }
                case "AddParkingSpace":
                    {
                        returnString = AddParkingSpace(Request, sl);
                        break;
                    }
                case "GetParkingSpaceInfoById":
                    {
                        returnString = GetParkingSpaceInfoById(Request, sl);
                        break;
                    }
                case "UpdateParkingSpace":
                    {
                        returnString = UpdateParkingSpace(Request, sl);
                        break;
                    }
            }

            context.Response.Write(returnString);
            context.Response.End();
        }

        private string UpdateParkingSpace(HttpRequest Request, cn.linkit.epark.ServiceList sl)
        {
            string result = string.Empty;
            string id = Request.Params["id"];
            string cityId = Request.Params["cityId"];
            string areaId = Request.Params["areaId"];
            string streetId = Request.Params["streetId"];
            string name = Request.Params["name"];
            string sensorNo = Request.Params["sensorCode"];
            string connection = Request.Params["connection"];
            string berthModelId = Request.Params["berthModel"];
            string berthTypeId = Request.Params["berthType"];
            string restrictionId = Request.Params["restriction"];
            string feeRuleId = Request.Params["feeRule"];
            string continuousLongId = Request.Params["continuousLong"];
            string longitude = Request.Params["longitude"];
            string latitude = Request.Params["latitude"];


            bool flag = true; //sl.UpdateParkingSpace(id, cityId, areaId, streetId, name, longitude, latitude, sensorNo, connection, berthModelId, berthTypeId, restrictionId, feeRuleId, continuousLongId);

            result = "{" + string.Format("\"isSuccess\":\"{0}\"", flag.ToString().ToLower()) + "}";

            return result;
        }

        private string GetParkingSpaceInfoById(HttpRequest Request, cn.linkit.epark.ServiceList sl)
        {
            string result = string.Empty;
            string id = Request.Params["id"];
            //result = sl.GetParkingSpaceById(id);

            result = "{\"ParkingSpaceInfo\":{\"ID\" :\"1\", \"CityId\" :\"2\",\"AreaId\":\"2002\",\"StreetId\":\"1010\", "
                + "\"Name\":\"车位1\", \"Longitude\":\"15.245\", \"Latitude\":\"78.256\", \"SensorNo\":\"TSN4515\", "
                + "\"Connection\":\"18685987587\", \"BerthModelId\":\"2\", \"BerthTypeId\":\"2\", \"RestrictionId\":\"2\","
                + "\"FeeRuleId\":\"2\", \"ContinuousLongId\":\"2\"}}";

            return result;
        }
        #region 车位管理列表
        private string ListContinuousLong(cn.linkit.epark.ServiceList sl)
        {
            //string json = sl.ListContinuousLong();
            string json = "{ \"ContinuousLong\": [{ \"id\": \"1\", \"Name\": \"泊位车型1\" }, { \"id\": \"2\", \"Name\": \"泊位车型2\"}] }";

            return json;
        }

        private string ListFeeRules(cn.linkit.epark.ServiceList sl)
        {
            //string json = sl.ListFeeRules();
            string json = "{ \"FeeRules\": [{ \"id\": \"1\", \"Name\": \"泊位车型1\" }, { \"id\": \"2\", \"Name\": \"泊位车型2\"}] }";

            return json;
        }

        private string ListRestrictions(cn.linkit.epark.ServiceList sl)
        {
            //string json = sl.ListRestrictions();
            string json = "{ \"Restrictions\": [{ \"id\": \"1\", \"Name\": \"泊位车型1\" }, { \"id\": \"2\", \"Name\": \"泊位车型2\"}] }";

            return json;
        }

        private string ListBerthType(cn.linkit.epark.ServiceList sl)
        {
            //string json = sl.ListBerthType();
            string json = "{ \"BerthType\": [{ \"id\": \"1\", \"Name\": \"泊位车型1\" }, { \"id\": \"2\", \"Name\": \"泊位车型2\"}] }";

            return json;
        }

        private string ListBerthModels(cn.linkit.epark.ServiceList sl)
        {
            //string json = sl.ListBerthModels();
            string json = "{ \"BerthModels\": [{ \"id\": \"1\", \"Name\": \"泊位车型1\" }, { \"id\": \"2\", \"Name\": \"泊位车型2\"}] }";

            return json;
        }

        private string DeleteParkingSpace(HttpRequest Request, cn.linkit.epark.ServiceList sl)
        {
            string id = Request.Params["id"];

            //bool flag = sl.DeleteParkingSpace(id);
            bool flag = true;
            string result = "{" + string.Format("\"isSuccess\":\"{0}\"", flag.ToString().ToLower()) + "}";
            return result;
        }

        private string ListParkingSpaces(HttpRequest Request, cn.linkit.epark.ServiceList sl)
        {

            int cityId = Utils.IsInt(Request.Params["cityId"]) ? int.Parse(Request.Params["cityId"]) : 0;
            int areaId = Utils.IsInt(Request.Params["areaId"]) ? int.Parse(Request.Params["areaId"]) : 0;
            int streetId = Utils.IsInt(Request.Params["streetId"]) ? int.Parse(Request.Params["streetId"]) : 0;

            string name = Request.Params["name"];
            int page = int.Parse(Request.Params["page"]);
            int size = int.Parse(Request.Params["size"]);

            //string result = sl.ListParkingSpaces(cityId, areaId, streetId, name, page, size);

            string result = ""
                + "{\"ListParkingSpaces\": ["
                + "    {"
                + "        \"ID\":\"1\","
                + "        \"Name\":\"西街车位2\","
                + "        \"StreetCode\":\"4516592\","
                + "        \"Longitude\":\"15.26\","
                + "        \"Latitude\":\"65.31\","
                + "        \"SensorCode\":\"SN4689125\","
                + "        \"Connection\":\"18627870000\","
                + "        \"CurrentState\":\"有车\","
                + "        \"BerthModels\":\"大型车\","
                + "        \"BerthType\":\"收费车位\","
                + "        \"Restrictions\":\"A2\","
                + "        \"FeeRules\":\"D3\","
                + "        \"ContinuousLong\":\"C4\""
                + "    },"
                + "    {"
                + "        \"ID\":\"2\","
                + "        \"Name\":\"西街车位2\","
                + "        \"StreetCode\":\"4516592\","
                + "        \"Longitude\":\"15.26\","
                + "        \"Latitude\":\"65.31\","
                + "        \"SensorCode\":\"SN4689125\","
                + "        \"Connection\":\"18627870000\","
                + "        \"CurrentState\":\"有车\","
                + "        \"BerthModels\":\"大型车\","
                + "        \"BerthType\":\"收费车位\","
                + "        \"Restrictions\":\"A2\","
                + "        \"FeeRules\":\"D3\","
                + "        \"ContinuousLong\":\"C4\""
                + "    }],\"RowsCount\":\"50\"}";

            return result;
        }
        #endregion 

        // 添加车位信息
        private string AddParkingSpace(HttpRequest Request, cn.linkit.epark.ServiceList sl)
        {
            string result = string.Empty;
            string cityId = Request.Params["cityId"];
            string areaId = Request.Params["areaId"];
            string streetId = Request.Params["streetId"];
            string name = Request.Params["name"];
            string sensorNo = Request.Params["sensorCode"];
            string connection = Request.Params["connection"];
            string berthModelId = Request.Params["berthModel"];
            string berthTypeId = Request.Params["berthType"];
            string restrictionId = Request.Params["restriction"];
            string feeRuleId = Request.Params["feeRule"];
            string continuousLongId = Request.Params["continuousLong"];
            string longitude = Request.Params["longitude"];
            string latitude = Request.Params["latitude"];


            bool flag = true; //sl.AddParkingSpace(cityId, areaId, streetId, name, longitude, latitude, sensorNo, connection, berthModelId, berthTypeId, restrictionId, feeRuleId, continuousLongId);

            result = "{" + string.Format("\"isSuccess\":\"{0}\"", flag.ToString().ToLower()) + "}";

            return result;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}